package online.longzipeng.householdbillssystem.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import online.longzipeng.householdbillssystem.business.entity.Bill;
import online.longzipeng.householdbillssystem.business.entity.Permiss;
import online.longzipeng.householdbillssystem.business.vo.permiss.PermissSaveVO;
import online.longzipeng.householdbillssystem.business.vo.permiss.PermissUpdateVO;
import online.longzipeng.householdbillssystem.business.vo.querey.QueryVO;
import online.longzipeng.householdbillssystem.common.result.ResponseEnum;
import online.longzipeng.householdbillssystem.common.result.Result;
import online.longzipeng.householdbillssystem.common.utils.ExceptionUtils;
import online.longzipeng.householdbillssystem.service.PermissService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lzp
 * @since 2021-04-17
 */
@RestController
@RequestMapping("/permiss")
@Api(value = "权限中的用户相关接口",description = "权限中的用户相关接口",tags = "权限中的用户相关接口")
@Slf4j
public class PermissController {

    @Autowired
    private PermissService PermissService;

    @PostMapping("/save")
    @ApiOperation("新建保存权限信息")
    public Result save(@Valid @RequestBody PermissSaveVO permissSaveVO){
        Permiss Permiss = new Permiss();
        BeanUtils.copyProperties(permissSaveVO,Permiss);
        ExceptionUtils.isTrue(PermissService.add(Permiss),"权限保存失败！！请检查填写的权限信息",Permiss);
        if (log.isDebugEnabled()){
            log.info(String.format("新建权限成功,权限信息：%s",Permiss.toString()));
        }
        return new Result(ResponseEnum.SUCCESS);
    }

    @DeleteMapping("/delete_one/{id}")
    @ApiOperation("删除单个权限信息")
    public Result deleteOne(@ApiParam(value="权限id",required = true) @PathVariable("id") Integer id){
        ExceptionUtils.isTrue(PermissService.delete(id),"删除失败！！参数不合法或已被其他用户删除",id);
        if (log.isDebugEnabled()){
            log.info(String.format("权限删除成功,权限信息：%d",id));
        }
        return new Result(ResponseEnum.SUCCESS);
    }

    @PostMapping("/delete")
    @ApiOperation("删除多个权限信息")
    public Result delete(@ApiParam(value="权限id数组",required = true, example = "1,2,3,4") @RequestParam("ids") Integer[] ids){
        for (Integer id : ids) {
            ExceptionUtils.isTrue(PermissService.delete(id),"删除失败！！参数不合法或已被其他用户删除",ids);
        }
        if (log.isDebugEnabled()){
            log.info(String.format("权限批量删除成功，ids：%s",ids.toString()));
        }
        return new Result(ResponseEnum.SUCCESS);
    }

    @PutMapping("/update")
    @ApiOperation("修改权限信息")
    public Result update(@Valid @RequestBody PermissUpdateVO permissUpdateVO){
        Permiss Permiss = new Permiss();
        BeanUtils.copyProperties(permissUpdateVO,Permiss);
        ExceptionUtils.isTrue(PermissService.edit(Permiss),"更新失败！！请检查输入的数据",Permiss);
        if (log.isDebugEnabled()){
            log.info(String.format("修改权限信息成功，权限信息：%s",Permiss.toString()));
        }
        return new Result(ResponseEnum.SUCCESS);
    }

}
